package com.unism.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PublicMethod {

	public static boolean isEmpty(String Value) {
		return (Value == null || Value.trim().equals("") || "undefined"
				.equals(Value));
	}

	public static boolean isEmpty(List list) {
		if (list == null || list.size() == 0)
			return true;
		else
			return false;
	}

	public static boolean isEmpty(Set set) {
		if (set == null || set.size() == 0)
			return true;
		else
			return false;
	}

	public static boolean isEmpty(Map map) {
		if (map == null || map.size() == 0)
			return true;
		else
			return false;
	}
	public static boolean isEmpty(Object Value) {
		if (Value == null)
			return true;
		else
			return false;
	}

	public static boolean isEmpty(Long obj) {
		if (obj == null || obj.longValue() == 0)
			return true;
		else
			return false;
	}

	public static boolean isEmpty(Object[] Value) {
		if (Value == null || Value.length == 0)
			return true;
		else
			return false;
	}

	/**
	 */
	public static String dateToString(Date dteValue, String strFormat) {
		SimpleDateFormat clsFormat = new SimpleDateFormat(strFormat);
		return clsFormat.format(dteValue);
	}

	/**
	 */
	public static String dateToDefaultString(Date dteValue) {
		if (dteValue == null) {
			return "";
		}
		SimpleDateFormat clsFormat = new SimpleDateFormat("yyyy-MM-dd");
		return clsFormat.format(dteValue);
	}

	/**
	 */
	public static String datetimeToDefaultString(Date dteValue) {
		if (dteValue == null) {
			return "";
		}
		SimpleDateFormat clsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return clsFormat.format(dteValue);
	}

	/**
	 */
	public static Date stringToDate(String strValue) {
		if (PublicMethod.isEmpty(strValue)) {
			return null;
		}
		SimpleDateFormat clsFormat = null;
		if (strValue.length() > 19)
			strValue = strValue.substring(0, 19);
		if (strValue.length() == 19)
			clsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		else if (strValue.length() == 17)
			clsFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		else if (strValue.length() == 10)
			clsFormat = new SimpleDateFormat("yyyy-MM-dd");
		else if (strValue.length() == 14)
			clsFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		
		else if (strValue.length() == 8)
			clsFormat = new SimpleDateFormat("yyyyMMdd");

		ParsePosition pos = new ParsePosition(0);
		return clsFormat.parse(strValue, pos);
	}

	public static Date stringToDate(String strValue, String dateformat) {
		if (PublicMethod.isEmpty(strValue)) {
			return null;
		}
		SimpleDateFormat clsFormat = new SimpleDateFormat(dateformat);

		ParsePosition pos = new ParsePosition(0);
		return clsFormat.parse(strValue, pos);
	}

	
	public static String moneyFormat(String s) {
		try {
			String s1 = "";
			String s2 = "";
			String s3 = "";
			if (s.indexOf(",") != -1)
				return s;
			if (s.equals(""))
				return "";
			if (s.equals("0"))
				return "0.00";
			if (s.substring(0, 1).equals("-")) {
				s3 = "-";
				s = s.substring(1, s.length());
			}
			if (s.substring(0, 1).equals("."))
				s = "0" + s;
			int i = s.indexOf("E");
			if (i != -1) {
				int j = Integer.valueOf(s.substring(i + 1, s.length()))
						.intValue();
				String s4 = s.substring(0, i);
				if (j > 0) {
					if (s4.length() - 2 <= j) {
						int l = (j - (s4.length() - 2)) + 1;
						for (int i1 = 0; i1 < l; i1++)
							s4 = s4 + "0";

					}
					String s7 = s4.substring(0, j + 2) + "."
							+ s4.substring(j + 2, s4.length());
					s4 = s7.substring(0, 1) + s7.substring(2, s7.length());
				} else {
					j = Math.abs(j);
					String s8 = s4.substring(0, 1)
							+ s4.substring(2, s4.length());
					s4 = "0.";
					for (int j1 = 0; j1 < j - 1; j1++)
						s4 = s4 + "0";

					s4 = s4 + s8;
				}
				s = s4;
			}
			int k = s.indexOf(".");
			if (k != -1) {
				s1 = s.substring(0, k);
				s2 = s.substring(k + 1, s.length());
			} else {
				s1 = s;
				s2 = "00";
			}
			if (s2.length() > 2) {
				String s5 = s2.substring(0, 2);
				String s9 = s2.substring(2, 3);
				int k1 = Integer.valueOf(s9).intValue();
				boolean flag = false;
				if (k1 >= 5) {
					int l1 = Integer.valueOf(s5).intValue();
					if (l1 <= 8) {
						l1++;
						s2 = "0" + String.valueOf(l1);
					} else if (++l1 >= 100) {
						String s11 = String.valueOf(l1);
						s2 = s11.substring(1, 3);
						long l2 = Long.valueOf(s1).longValue();
						l2++;
						s1 = String.valueOf(l2);
					} else {
						s2 = String.valueOf(l1);
					}
				} else {
					s2 = s5;
				}
			}
			if (s1.length() > 3) {
				String s6 = s1;
				String s10 = "";
				for (; s6.length() > 3; s6 = s6.substring(0, s6.length() - 3))
					s10 = "," + s6.substring(s6.length() - 3, s6.length())
							+ s10;

				s10 = s6 + s10;
				s1 = s10;
			}
			if (s2.length() == 1)
				s2 = s2 + "0";
			return s3 + s1 + "." + s2;
		} catch (Exception e) {
			e.printStackTrace();
			return s;
		}
	}
	
	/**
	 * 
	 * 
	 * @param date1
	 * @param date2
	 * @return 如果date1 大于或者等于 date2返回true 否则返回false
	 */
	public static boolean compareDate(Date date1,Date date2){
		int after = date1.compareTo(date2);
		if(after ==0 ||after > 0){
		  return true;
		}else{
		  return false;
		}
	}

	

	/**
	 * @param e
	 * @return
	 */
	public static String getErrorDetail(Exception e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		return sw.toString();
	}

	/**
	 * In 0.00,33.33333 Out33.33
	 * 
	 * @param format
	 * @param value
	 * @return
	 */
	public static String converterNumberString(String format, Double value) {
		DecimalFormat df = new DecimalFormat(format);
		return df.format(value);
	}
	
	/**
	 * In 0.00,33.33333 Out33.33
	 * 
	 * @param format
	 * @param value
	 * @return
	 */
	public static double converterNumberDouble(String format, Double value) {
		DecimalFormat df = new DecimalFormat(format);
		double formatDouble = Double.parseDouble(df.format(value));
		if(formatDouble ==0.000){//0流量
			return 0.0;
		}else if(formatDouble < 0.25){//小于1MB时显示基础流量线 <100K 0.1
			return 0.1;
		}else if(formatDouble >= 0.25 && formatDouble < 5.0 ){//以50MB为0.1步进  0.1~2.0
			return formatDouble/2.5;
		}else if(formatDouble >= 5.0 && formatDouble <10.0  ){//以50MB为0.1步进 2.0~4.0
			return formatDouble/2.5;
		}else{//大于500Mb时显示5倍粗流量线 5.0
			return 5.0;
		}
		 
	}
	
	public static void main(String [] args){
		System.out.println(PublicMethod.converterNumberDouble("0.0000000", 0.0000000954));//1byte
		System.out.println(PublicMethod.converterNumberDouble("0.0000000", 0.0000000054));//<1byte
		
		System.out.println(new Integer(1).doubleValue()/1024/1024/10);//1 byte
		System.out.println(PublicMethod.converterNumberDouble("0.0000000",new Integer(1).doubleValue()/1024/1024/10));//1byte
		System.out.println(PublicMethod.converterNumberDouble("0.0000000",new Integer(0).doubleValue()/1024/1024/10));//0byte
	}
	
	/**
	 * @param valueTo
	 * @param valueFrom
	 * @return
	 */
	public static Object copyValue(Object valueTo, Object valueFrom) {
		try {
			Class clsTo = valueTo.getClass();
			Class clsFrom = valueFrom.getClass();
			Field[] toFields = clsTo.getDeclaredFields();
			for (int i = 0; i < toFields.length; i++) {
				Field field = toFields[i];
				Class toType = field.getType();
				String fileName = field.getName().substring(0, 1).toUpperCase()
						+ field.getName().substring(1);
				String getMethodName = "get" + fileName;
				String setMethodName = "set" + fileName;
				Field fromField = null;
				try {
					fromField = clsFrom.getDeclaredField(field.getName());
				} catch (java.lang.NoSuchFieldException ex) {
					continue;
				}
				if (fromField != null) {
					Class type = fromField.getType();
					if (toType == type) {
						Method fromGetMethod = clsFrom.getMethod(getMethodName);
						Object filedValue = fromGetMethod.invoke(valueFrom,
								new Object[] {});
						Method toSetMethod = clsTo.getMethod(setMethodName,
								toType);
						toSetMethod.invoke(valueTo, filedValue);
					}
				}
			}
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
		return valueTo;
	}
	public static double getFloorNumber(double d){
		double tmp = d - Math.floor(d);
		if(tmp == 0.0){
			return d;
		}else{
			return Math.floor(d)+1.0;
		}
	}
	
	public static String getLabelByBtye(int bytes){
		if(bytes<=0){
			return "";
		}
		int i = bytes/1024;
		if(i==0){
			return bytes+"b";
		}
		if(i>=1024){
			double j = new Integer(i).doubleValue()/1024;
			return converterNumberString("0.0",j)+"M";
		}else{
			return i+"k";
		}
	}
	
	public static String getLabelByBtye(long bytes){
		if(bytes<=0){
			return "";
		}
		long i = bytes/1024;
		if(i==0){
			return bytes+"b";
		}
		if(i>=1024){
			double j = new Long(i).doubleValue()/1024;
			return converterNumberString("0.0",j)+"M";
		}else{
			return i+"k";
		}
	}

	/**
	 * 
	 * ip 验证
	 * @param ip
	 * @return
	 */
	public static boolean isIp(String ip){
		Pattern patt = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
	    Matcher mat = patt.matcher(ip);
	    return mat.matches();
	}

	public static Date changeStrToDate(String timeStr, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date result = null;
		try {
			result = sdf.parse(timeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}
}
